package com.work.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.work.common.result.Result;
import com.work.common.result.ResultType;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import java.io.IOException;

public class AuthFailFilter implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setCharacterEncoding("UTF-8");
        Result<String> result = Result.fail(ResultType.FORBIDDEN);
        ObjectMapper mapper = new ObjectMapper();
        String value = mapper.writeValueAsString(result);
        response.getWriter().write(value);
    }
}
